﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShoppingCart : System.Web.UI.Page
{
    int newEdit;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["list"] == null)
            {
                List<CartItem> list = new List<CartItem>();
                Session["list"] = list;
            }
            List<CartItem> list1 = (List<CartItem>)Session["list"];
            string cateID = Request["cateID"];
            if (cateID != "empty")
            {
                bool isExisted = false;
                foreach (CartItem c in list1)
                {
                    if (c.ItemID.ToString() == cateID)
                    {
                        c.Quantity += 1;
                        isExisted = true;
                        break;
                    }

                }
                if (!isExisted)
                {
                    BookStoreDataContext bs = new BookStoreDataContext();
                    var cartItem = (from cart in bs.items where cart.item_id.ToString() == cateID select cart);
                    if (cartItem.Count<item>() > 0)
                    {
                        item item = cartItem.First();
                        CartItem c = new CartItem();
                        c.ItemID = item.item_id;
                        c.ItemName = item.name;
                        c.Price = item.price;
                        c.Quantity = 1;
                        //c.Amount = c.Quantity * c.Price;
                        list1.Add(c);
                        Session["list"] = list1;
                    }

                }
            }
            loadData();
        }
    }

    private void loadData()
    {
        List<CartItem> list1 = (List<CartItem>)Session["list"];
        grvShoppingCart.DataSource = list1;
        grvShoppingCart.DataBind();

    }
    protected void grvShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        string id = grvShoppingCart.DataKeys[e.RowIndex].Values[0].ToString();
        Console.WriteLine(id);
        List<CartItem> list = (List<CartItem>)Session["list"];
        foreach (CartItem c in list)
        {
            if (c.ItemID.ToString() == id)
            {
                list.Remove(c);
                break;
            }
        }
        Session["list"] = list;
        grvShoppingCart.DataSource = list;
        grvShoppingCart.DataBind();

    }
    protected void grvShoppingCart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvShoppingCart.EditIndex = e.NewEditIndex;
        newEdit = e.NewEditIndex;
        loadData();

    }
    protected void grvShoppingCart_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void grvShoppingCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //string s = grvShoppingCart.DataKeys[newEdit].Value.ToString();
        //int index = e.RowIndex;
        //.WriteLine(index.ToString());
        string cartID = e.Keys[0].ToString();
        List<CartItem> list = (List<CartItem>)Session["list"];
        //foreach (CartItem cart in list)
        //{
        //    if (cart.ItemID.ToString() == cartID) { 
        //    cart.ItemName = 
        //    }
        Session["list"] = grvShoppingCart.DataSource;
        grvShoppingCart.EditIndex = -1;
        loadData();

    }
    protected void grvShoppingCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvShoppingCart.EditIndex = -1;
        loadData();
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        Response.Redirect("Checkout.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        //if (Request.UrlReferrer != null)
        //{
        string referer = Request.UrlReferrer.ToString();
        Response.Redirect(referer);
        //}

    }
}